import tools.load_django_env

from book.models import BookInfo, HeroInfo
from book.serializers import BookInfoSerializers, HeroInfoSerializers

if __name__ == '__main__':
    # 序列化测试
    # book = BookInfo.objects.get(id=1)
    # t = BookInfoSerializers(book)
    # print(t.data)
    #
    # hero = HeroInfo.objects.get(id=1)
    # t = HeroInfoSerializers(hero)
    # print(t.data)

    # 反序列化测试
    # books_data = [
    #     {'btitle': '红楼梦', 'bpub_date': '2019-06-14'},
    #     {'btitle': '金瓶梅', 'bpub_date': '2019-06-14', 'bread': 1, 'bcomment': 1},
    # ]
    # for data in books_data:
    #     serializer = BookInfoSerializers(data=data)
    #     result = serializer.is_valid(raise_exception=True)
    #     if not result:
    #         print('验证失败')
    #         print(serializer.errors)
    #     else:
    #         print('验证成功')
    #         print(serializer.validated_data)
    #         serializer.save()


    # 保存英雄
    hero = {'hname': '贾宝玉', 'hbook_id1': 11}
    serializer = HeroInfoSerializers(data=hero)
    result = serializer.is_valid(raise_exception=True)
    serializer.save()
    print(serializer.data)

    # 读取英雄
    # hero = HeroInfo.objects.get(id=1)
    # t = HeroInfoSerializers(hero)
    # print(t.data)

    pass
